library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(htmlwidgets)
library(patchwork)
data <- read.csv("combined_all_means_with_state.csv")
data$state = data$State
race_data <- data %>%
  select(starts_with("Bene_Race"), starts_with("State")) %>%
  select(where(~!any(is.na(.))))

race_columns <- grep("Bene_Race", names(race_data), value = TRUE)

race_data_cnt <- race_data %>%
  mutate(Total_Benes = rowSums(select(., all_of(race_columns))))

race_data_pct <- race_data_cnt %>%
  mutate(across(all_of(race_columns), ~ .x / Total_Benes * 100, .names = "{.col}_Pct")) %>%
  select(ends_with("Pct"), ends_with("State"))

race_data_pct$state = race_data_pct$State
us_map <- map_data("state")
state_lookup <- data.frame(state = c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"),
                           state_name = c("alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "west virginia", "wisconsin", "wyoming"))
aggregate_df1 <- merge(race_data_pct, state_lookup, by="state", all.x=TRUE)
aggregate_df1 <- aggregate_df1[!is.na(aggregate_df1$state_name), ]
merged_data1 <- merge(us_map, aggregate_df1, by.x="region", by.y="state_name", all.x=TRUE)

race_pct_columns <- grep("_Pct$", names(merged_data1), value = TRUE)
cc_data <- data %>%
  select(starts_with("Bene_CC"), starts_with("state")) %>%
  select(where(~!any(is.na(.))))

cc_columns <- grep("Bene_CC", names(cc_data), value = TRUE)
aggregate_df2 <- merge(cc_data, state_lookup, by="state", all.x=TRUE)
aggregate_df2 <- aggregate_df2[!is.na(aggregate_df2$state_name), ]
merged_data2 <- merge(us_map, aggregate_df2, by.x="region", by.y="state_name", all.x=TRUE)
pAF <- ggplot(merged_data2, aes(x=long, y=lat, group=group,
                               fill=Bene_CC_AF_Pct)) +
    geom_polygon(color="black") +
    scale_fill_gradient(low="lightblue1", high="deepskyblue3", na.value="grey") +
    labs(fill="Bene_CC_AF_Pct") +  
    coord_map() +
    theme_void() +
    ggtitle("Map of Beneficiaries Identified With Atrial Fibrillation Percentage") 

pAF <- pAF + theme(legend.text = element_text(size = 3),
                 legend.title = element_text(size = 3),
                 legend.position = "bottom",
                 legend.key.size = unit(0.3, "cm"),
                 legend.spacing = unit(0.1, "cm"))
pAF <- ggplotly(pAF)
library(htmltools)
htmltools::div(style = "display: flex;",
  htmltools::div(style = "width: 50%;", pW),
  htmltools::div(style = "width: 50%;", pAF)
)
htmltools::div(style = "display: flex;",
  htmltools::div(style = "width: 50%;", pH),
  htmltools::div(style = "width: 50%;", pDb)
)
htmltools::div(style = "display: flex;",
  htmltools::div(style = "width: 50%;", pN),
  htmltools::div(style = "width: 50%;", pAs)
)
htmltools::div(style = "display: flex;",
  htmltools::div(style = "width: 50%;", pA),
  htmltools::div(style = "width: 50%;", pCK)
)